package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.StringUtils;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class StatementItemServiceImpl extends
        ServiceImpl<StatementItemMapper,StatementItem> implements IStatementItemService {
    @Autowired
    private IStatementService statementService;

    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        IPage<StatementItem> page = new Page<>(qo.getPageNum(),qo.getPageSize());
        QueryWrapper<StatementItem> wrapper = new QueryWrapper<>();
            wrapper.eq("statement_id",qo.getStatementId());
        return new TablePageInfo<StatementItem>(super.page(page,wrapper));
    }
    @Override
    public void saveDetail(List<StatementItem> items) {
        //删除并获取最后一条的记录
        StatementItem temp = items.remove(items.size() - 1);
        //获取优惠价格
        BigDecimal discountPrice = temp.getItemPrice();
        //获取优惠价价格和结算单Id
        BigDecimal totalAmount=new BigDecimal("0.00");
        BigDecimal totalQuantity=new BigDecimal("0.0");
        //处于消费中才能保存明细
        Statement statement = statementService.getById(temp.getStatementId());
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())){
            //删除明细项目
            Map<String,Object> map =new HashMap<>();
            map.put("statement_id",temp.getStatementId());
            this.removeByMap(map);
            for (StatementItem item : items) {
                totalAmount=totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
                totalQuantity=totalQuantity.add(item.getItemQuantity());
                //循环过程中把明细项插入明细表中
                item.setStatementId(temp.getStatementId());
                this.save(item);
            }
        }
        //判断折扣金额的合理性
        if (totalAmount.compareTo(discountPrice)<0){
            throw new BusinessException("优惠金额大于实付金额");
        }
        statementService.updateOtherPrice(ShiroUtils.getUserId(),statement.getId(),totalAmount,totalQuantity,discountPrice);
    }

}
